Added some more fields to host_cpu.
authorEwan Mellor <ewan@xensource.com>
Tue, 27 Feb 2007 00:37:27 +0000 (00:37 +0000)
committerEwan Mellor <ewan@xensource.com>
Tue, 27 Feb 2007 00:37:27 +0000 (00:37 +0000)
Signed-off-by: Ewan Mellor <ewan@xensource.com>
docs/xen-api/xenapi-datamodel.tex
tools/libxen/include/xen_host_cpu.h
tools/libxen/src/xen_host_cpu.c
tools/python/xen/xend/XendAPI.py
tools/python/xen/xend/XendNode.py

index 420f0ae6183fef081c163f2dfc2a9d061f917edc..4c07eaadf0e9ba43c34a7e0cacc895944346bd11 100644 (file)
@@ -6278,11 +6278,13 @@ all fields from the object
 Quals & Field & Type & Description \\
 \hline
 $\mathit{RO}_\mathit{run}$ &  {\tt uuid} & string & unique identifier/object reference \\
-$\mathit{RO}_\mathit{ins}$ &  {\tt host} & host ref & the host the CPU is in \\
-$\mathit{RO}_\mathit{ins}$ &  {\tt number} & int & the number of the physical CPU within the host \\
-$\mathit{RO}_\mathit{ins}$ &  {\tt vendor} & string & the vendor of the physical CPU \\
-$\mathit{RO}_\mathit{ins}$ &  {\tt speed} & int & the speed of the physical CPU \\
-$\mathit{RO}_\mathit{ins}$ &  {\tt modelname} & string & the model name of the physical CPU \\
+$\mathit{RO}_\mathit{run}$ &  {\tt host} & host ref & the host the CPU is in \\
+$\mathit{RO}_\mathit{run}$ &  {\tt number} & int & the number of the physical CPU within the host \\
+$\mathit{RO}_\mathit{run}$ &  {\tt vendor} & string & the vendor of the physical CPU \\
+$\mathit{RO}_\mathit{run}$ &  {\tt speed} & int & the speed of the physical CPU \\
+$\mathit{RO}_\mathit{run}$ &  {\tt modelname} & string & the model name of the physical CPU \\
+$\mathit{RO}_\mathit{run}$ &  {\tt stepping} & string & the stepping of the physical CPU \\
+$\mathit{RO}_\mathit{run}$ &  {\tt flags} & string & the flags of the physical CPU \\
 $\mathit{RO}_\mathit{run}$ &  {\tt utilisation} & float & the current CPU utilisation \\
 \hline
 \end{longtable}
@@ -6479,13 +6481,13 @@ value of the field
 \vspace{0.3cm}
 \vspace{0.3cm}
 \vspace{0.3cm}
-\subsubsection{RPC name:~get\_utilisation}
+\subsubsection{RPC name:~get\_stepping}
 
 {\bf Overview:} 
-Get the utilisation field of the given host\_cpu.
+Get the stepping field of the given host\_cpu.
 
  \noindent {\bf Signature:} 
-\begin{verbatim} float get_utilisation (session_id s, host_cpu ref self)\end{verbatim}
+\begin{verbatim} string get_stepping (session_id s, host_cpu ref self)\end{verbatim}
 
 
 \noindent{\bf Arguments:}
@@ -6503,7 +6505,7 @@ Get the utilisation field of the given host\_cpu.
 
  \noindent {\bf Return Type:} 
 {\tt 
-float
+string
 }
 
 
@@ -6511,13 +6513,13 @@ value of the field
 \vspace{0.3cm}
 \vspace{0.3cm}
 \vspace{0.3cm}
-\subsubsection{RPC name:~create}
+\subsubsection{RPC name:~get\_flags}
 
 {\bf Overview:} 
-Create a new host\_cpu instance, and return its handle.
+Get the flags field of the given host\_cpu.
 
  \noindent {\bf Signature:} 
-\begin{verbatim} (host_cpu ref) create (session_id s, host_cpu record args)\end{verbatim}
+\begin{verbatim} string get_flags (session_id s, host_cpu ref self)\end{verbatim}
 
 
 \noindent{\bf Arguments:}
@@ -6527,7 +6529,7 @@ Create a new host\_cpu instance, and return its handle.
 \begin{tabular}{|c|c|p{7cm}|}
  \hline
 {\bf type} & {\bf name} & {\bf description} \\ \hline
-{\tt host\_cpu record } & args & All constructor arguments \\ \hline 
+{\tt host\_cpu ref } & self & reference to the object \\ \hline 
 
 \end{tabular}
 
@@ -6535,21 +6537,21 @@ Create a new host\_cpu instance, and return its handle.
 
  \noindent {\bf Return Type:} 
 {\tt 
-host\_cpu ref
+string
 }
 
 
-reference to the newly created object
+value of the field
 \vspace{0.3cm}
 \vspace{0.3cm}
 \vspace{0.3cm}
-\subsubsection{RPC name:~destroy}
+\subsubsection{RPC name:~get\_utilisation}
 
 {\bf Overview:} 
-Destroy the specified host\_cpu instance.
+Get the utilisation field of the given host\_cpu.
 
  \noindent {\bf Signature:} 
-\begin{verbatim} void destroy (session_id s, host_cpu ref self)\end{verbatim}
+\begin{verbatim} float get_utilisation (session_id s, host_cpu ref self)\end{verbatim}
 
 
 \noindent{\bf Arguments:}
@@ -6567,11 +6569,11 @@ Destroy the specified host\_cpu instance.
 
  \noindent {\bf Return Type:} 
 {\tt 
-void
+float
 }
 
 
-
+value of the field
 \vspace{0.3cm}
 \vspace{0.3cm}
 \vspace{0.3cm}
index 945a7279d177d20ed20692023d5ccd499c2f3d70..f70023f24e94e303666f4cee79c472d694f8ef1c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, XenSource Inc.
+ * Copyright (c) 2006-2007, XenSource Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -25,8 +25,8 @@
 
 
 /*
- * The host_cpu class. 
- *  
+ * The host_cpu class.
+ * 
  * A physical CPU.
  */
 
@@ -68,6 +68,8 @@ typedef struct xen_host_cpu_record
     char *vendor;
     int64_t speed;
     char *modelname;
+    char *stepping;
+    char *flags;
     double utilisation;
 } xen_host_cpu_record;
 
@@ -164,20 +166,6 @@ extern bool
 xen_host_cpu_get_by_uuid(xen_session *session, xen_host_cpu *result, char *uuid);
 
 
-/**
- * Create a new host_cpu instance, and return its handle.
- */
-extern bool
-xen_host_cpu_create(xen_session *session, xen_host_cpu *result, xen_host_cpu_record *record);
-
-
-/**
- * Destroy the specified host_cpu instance.
- */
-extern bool
-xen_host_cpu_destroy(xen_session *session, xen_host_cpu host_cpu);
-
-
 /**
  * Get the uuid field of the given host_cpu.
  */
@@ -220,6 +208,20 @@ extern bool
 xen_host_cpu_get_modelname(xen_session *session, char **result, xen_host_cpu host_cpu);
 
 
+/**
+ * Get the stepping field of the given host_cpu.
+ */
+extern bool
+xen_host_cpu_get_stepping(xen_session *session, char **result, xen_host_cpu host_cpu);
+
+
+/**
+ * Get the flags field of the given host_cpu.
+ */
+extern bool
+xen_host_cpu_get_flags(xen_session *session, char **result, xen_host_cpu host_cpu);
+
+
 /**
  * Get the utilisation field of the given host_cpu.
  */
index 1300ce0e436811b4189f67450114a919b3cc0062..425d0c60b974dcfd085cdc5920f985211e02c102 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, XenSource Inc.
+ * Copyright (c) 2006-2007, XenSource Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -55,6 +55,12 @@ static const struct_member xen_host_cpu_record_struct_members[] =
         { .key = "modelname",
           .type = &abstract_type_string,
           .offset = offsetof(xen_host_cpu_record, modelname) },
+        { .key = "stepping",
+          .type = &abstract_type_string,
+          .offset = offsetof(xen_host_cpu_record, stepping) },
+        { .key = "flags",
+          .type = &abstract_type_string,
+          .offset = offsetof(xen_host_cpu_record, flags) },
         { .key = "utilisation",
           .type = &abstract_type_float,
           .offset = offsetof(xen_host_cpu_record, utilisation) }
@@ -82,6 +88,8 @@ xen_host_cpu_record_free(xen_host_cpu_record *record)
     xen_host_record_opt_free(record->host);
     free(record->vendor);
     free(record->modelname);
+    free(record->stepping);
+    free(record->flags);
     free(record);
 }
 
@@ -127,24 +135,24 @@ xen_host_cpu_get_by_uuid(xen_session *session, xen_host_cpu *result, char *uuid)
 
 
 bool
-xen_host_cpu_create(xen_session *session, xen_host_cpu *result, xen_host_cpu_record *record)
+xen_host_cpu_get_host(xen_session *session, xen_host *result, xen_host_cpu host_cpu)
 {
     abstract_value param_values[] =
         {
-            { .type = &xen_host_cpu_record_abstract_type_,
-              .u.struct_val = record }
+            { .type = &abstract_type_string,
+              .u.string_val = host_cpu }
         };
 
     abstract_type result_type = abstract_type_string;
 
     *result = NULL;
-    XEN_CALL_("host_cpu.create");
+    XEN_CALL_("host_cpu.get_host");
     return session->ok;
 }
 
 
 bool
-xen_host_cpu_destroy(xen_session *session, xen_host_cpu host_cpu)
+xen_host_cpu_get_number(xen_session *session, int64_t *result, xen_host_cpu host_cpu)
 {
     abstract_value param_values[] =
         {
@@ -152,13 +160,15 @@ xen_host_cpu_destroy(xen_session *session, xen_host_cpu host_cpu)
               .u.string_val = host_cpu }
         };
 
-    xen_call_(session, "host_cpu.destroy", param_values, 1, NULL, NULL);
+    abstract_type result_type = abstract_type_int;
+
+    XEN_CALL_("host_cpu.get_number");
     return session->ok;
 }
 
 
 bool
-xen_host_cpu_get_host(xen_session *session, xen_host *result, xen_host_cpu host_cpu)
+xen_host_cpu_get_vendor(xen_session *session, char **result, xen_host_cpu host_cpu)
 {
     abstract_value param_values[] =
         {
@@ -169,13 +179,13 @@ xen_host_cpu_get_host(xen_session *session, xen_host *result, xen_host_cpu host_
     abstract_type result_type = abstract_type_string;
 
     *result = NULL;
-    XEN_CALL_("host_cpu.get_host");
+    XEN_CALL_("host_cpu.get_vendor");
     return session->ok;
 }
 
 
 bool
-xen_host_cpu_get_number(xen_session *session, int64_t *result, xen_host_cpu host_cpu)
+xen_host_cpu_get_speed(xen_session *session, int64_t *result, xen_host_cpu host_cpu)
 {
     abstract_value param_values[] =
         {
@@ -185,13 +195,13 @@ xen_host_cpu_get_number(xen_session *session, int64_t *result, xen_host_cpu host
 
     abstract_type result_type = abstract_type_int;
 
-    XEN_CALL_("host_cpu.get_number");
+    XEN_CALL_("host_cpu.get_speed");
     return session->ok;
 }
 
 
 bool
-xen_host_cpu_get_vendor(xen_session *session, char **result, xen_host_cpu host_cpu)
+xen_host_cpu_get_modelname(xen_session *session, char **result, xen_host_cpu host_cpu)
 {
     abstract_value param_values[] =
         {
@@ -202,13 +212,13 @@ xen_host_cpu_get_vendor(xen_session *session, char **result, xen_host_cpu host_c
     abstract_type result_type = abstract_type_string;
 
     *result = NULL;
-    XEN_CALL_("host_cpu.get_vendor");
+    XEN_CALL_("host_cpu.get_modelname");
     return session->ok;
 }
 
 
 bool
-xen_host_cpu_get_speed(xen_session *session, int64_t *result, xen_host_cpu host_cpu)
+xen_host_cpu_get_stepping(xen_session *session, char **result, xen_host_cpu host_cpu)
 {
     abstract_value param_values[] =
         {
@@ -216,15 +226,16 @@ xen_host_cpu_get_speed(xen_session *session, int64_t *result, xen_host_cpu host_
               .u.string_val = host_cpu }
         };
 
-    abstract_type result_type = abstract_type_int;
+    abstract_type result_type = abstract_type_string;
 
-    XEN_CALL_("host_cpu.get_speed");
+    *result = NULL;
+    XEN_CALL_("host_cpu.get_stepping");
     return session->ok;
 }
 
 
 bool
-xen_host_cpu_get_modelname(xen_session *session, char **result, xen_host_cpu host_cpu)
+xen_host_cpu_get_flags(xen_session *session, char **result, xen_host_cpu host_cpu)
 {
     abstract_value param_values[] =
         {
@@ -235,7 +246,7 @@ xen_host_cpu_get_modelname(xen_session *session, char **result, xen_host_cpu hos
     abstract_type result_type = abstract_type_string;
 
     *result = NULL;
-    XEN_CALL_("host_cpu.get_modelname");
+    XEN_CALL_("host_cpu.get_flags");
     return session->ok;
 }
 
index 3926cd7d75f14633cc6ea77711edc82e7c371880..3562ba45f09394652e2ef57a72e2bd70eeac6b8a 100644 (file)
@@ -753,27 +753,44 @@ class XendAPI(object):
 
     host_cpu_attr_ro = ['host',
                         'number',
+                        'vendor',
+                        'speed',
+                        'modelname',
+                        'stepping',
+                        'flags',
                         'utilisation']
 
     # attributes
-    def host_cpu_get_host(self, session, host_cpu_ref):
+    def _host_cpu_get(self, ref, field):
+        return xen_api_success(
+            XendNode.instance().get_host_cpu_field(ref, field))
+
+    def host_cpu_get_host(self, _, ref):
         return xen_api_success(XendNode.instance().uuid)
-    def host_cpu_get_utilisation(self, session, host_cpu_ref):
-        util = XendNode.instance().get_host_cpu_load(host_cpu_ref)
-        return xen_api_success(util)
-    def host_cpu_get_number(self, session, host_cpu_ref):
-        num = XendNode.instance().get_host_cpu_number(host_cpu_ref)
-        return xen_api_success(num)
+    def host_cpu_get_number(self, _, ref):
+        return self._host_cpu_get(ref, 'number')
+    def host_cpu_get_vendor(self, _, ref):
+        return self._host_cpu_get(ref, 'vendor')
+    def host_cpu_get_speed(self, _, ref):
+        return self._host_cpu_get(ref, 'speed')
+    def host_cpu_get_modelname(self, _, ref):
+        return self._host_cpu_get(ref, 'modelname')
+    def host_cpu_get_stepping(self, _, ref):
+        return self._host_cpu_get(ref, 'stepping')
+    def host_cpu_get_flags(self, _, ref):
+        return self._host_cpu_get(ref, 'flags')
+    def host_cpu_get_utilisation(self, _, ref):
+        return xen_api_success(XendNode.instance().get_host_cpu_load(ref))
 
     # object methods
-    def host_cpu_destroy(self, session, host_cpu_ref):
-        return xen_api_error(XEND_ERROR_UNSUPPORTED)
-    def host_cpu_get_record(self, session, host_cpu_ref):
+    def host_cpu_get_record(self, _, ref):
         node = XendNode.instance()
-        record = {'uuid': host_cpu_ref,
-                  'host': node.uuid,
-                  'number': node.get_host_cpu_number(host_cpu_ref),
-                  'utilisation': node.get_host_cpu_load(host_cpu_ref)}
+        record = dict([(f, node.get_host_cpu_field(ref, f))
+                       for f in self.host_cpu_attr_ro
+                       if f not in ['uuid', 'host', 'utilisation']])
+        record['uuid'] = ref
+        record['host'] = node.uuid
+        record['utilisation'] = node.get_host_cpu_load(ref)
         return xen_api_success(record)
 
     # class methods
index 4534dc57ff45078e9d0120ed9341631eb5b9c88a..f550a467a9eb7d646cda39cba58dada09d85e062 100644 (file)
@@ -81,7 +81,7 @@ class XendNode:
         for cpu_uuid, cpu in saved_cpus.items():
             self.cpus[cpu_uuid] = cpu
 
-        # verify we have enough cpus here
+        cpuinfo = parse_proc_cpuinfo()
         physinfo = self.physinfo_dict()
         cpu_count = physinfo['nr_cpus']
         cpu_features = physinfo['hw_caps']
@@ -91,12 +91,23 @@ class XendNode:
         if cpu_count != len(self.cpus):
             self.cpus = {}
             for i in range(cpu_count):
-                cpu_uuid = uuid.createString()
-                cpu_info = {'uuid': cpu_uuid,
-                            'host': self.uuid,
-                            'number': i,
-                            'features': cpu_features}
-                self.cpus[cpu_uuid] = cpu_info
+                u = uuid.createString()
+                self.cpus[u] = {'uuid': u, 'number': i }
+
+        for u in self.cpus.keys():
+            log.error(self.cpus[u])
+            number = self.cpus[u]['number']
+            log.error(number)
+            log.error(cpuinfo)
+            self.cpus[u].update(
+                { 'host'     : self.uuid,
+                  'features' : cpu_features,
+                  'speed'    : int(float(cpuinfo[number]['cpu MHz'])),
+                  'vendor'   : cpuinfo[number]['vendor_id'],
+                  'modelname': cpuinfo[number]['model name'],
+                  'stepping' : cpuinfo[number]['stepping'],
+                  'flags'    : cpuinfo[number]['flags'],
+                })
 
         self.pifs = {}
         self.pif_metrics = {}
@@ -368,18 +379,12 @@ class XendNode:
         else:
             raise XendError('Invalid CPU Reference')
 
-    def get_host_cpu_features(self, host_cpu_ref):
+    def get_host_cpu_field(self, ref, field):
         try:
-            return self.cpus[host_cpu_ref]['features']
+            return self.cpus[ref][field]
         except KeyError:
             raise XendError('Invalid CPU Reference')
 
-    def get_host_cpu_number(self, host_cpu_ref):
-        try:
-            return self.cpus[host_cpu_ref]['number']
-        except KeyError:
-            raise XendError('Invalid CPU Reference')        
-            
     def get_host_cpu_load(self, host_cpu_ref):
         host_cpu = self.cpus.get(host_cpu_ref)
         if not host_cpu:
@@ -542,6 +547,31 @@ class XendNode:
             pif.refresh(Brctl.get_state())
 
 
+def parse_proc_cpuinfo():
+    cpuinfo = {}
+    f = file('/proc/cpuinfo', 'r')
+    try:
+        p = -1
+        d = {}
+        for line in f:
+            keyvalue = line.split(':')
+            if len(keyvalue) != 2:
+                continue
+            key = keyvalue[0].strip()
+            val = keyvalue[1].strip()
+            if key == 'processor':
+                if p != -1:
+                    cpuinfo[p] = d
+                p = int(val)
+                d = {}
+            else:
+                d[key] = val
+        cpuinfo[p] = d
+        return cpuinfo
+    finally:
+        f.close()
+
+
 def instance():
     global inst
     try: